package com.lyf.learn.atsgg.mybatis;

import com.lyf.learn.atsgg.mybatis.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import java.io.IOException;
import java.io.InputStream;

/**
 * @AUTHOR LYF
 * @DATE 2022/5/28
 * @VERSION 1.0
 * @DESC
 * 启动类
 */
public class MainApp {
    public static void main(String[] args) throws IOException {
        // 工厂+构建模式→总结设计模式（源码分析）
        InputStream is = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory =  sqlSessionFactoryBuilder.build(is);

        // 会话
        SqlSession session = sqlSessionFactory.openSession();

        UserMapper userMapper = session.getMapper(UserMapper.class);
        int i = userMapper.addUser();
        session.commit();//未配置事务管理，需要手动进行提交
        System.out.println("RS:"+i);
    }
}
